package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.domain.DictDO;
import com.jinzhi.common.service.DictService;
import com.jinzhi.common.utils.Result;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzweb.domain.ExaminationPaperDO;
import com.jinzhi.jzweb.domain.OrganizationNewsDO;
import com.jinzhi.jzweb.domain.OrganizationRolesDO;
import com.jinzhi.jzweb.domain.RecruitmentNewsDO;
import com.jinzhi.jzweb.service.OrganizationNewsService;
import com.jinzhi.jzweb.service.OrganizationRolesService;
import com.jinzhi.jzweb.service.RecruitmentNewsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <pre>
 * 招聘信息
 * </pre>
 * <small> 2020-02-24 21:00:11 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/recruitmentNews")
public class RecruitmentNewsController extends BaseController {
    @Autowired
    private RecruitmentNewsService recruitmentNewsService;
    @Autowired
    private OrganizationNewsService organizationNewsService;
    @Autowired
    private OrganizationRolesService organizationRolesService;
    @Autowired
    private DictService sysDictService;
    @GetMapping()
    @RequiresPermissions("jzweb:recruitmentNews:recruitmentNews")
    String RecruitmentNews() {
        return "jzweb/recruitmentNews/recruitmentNews";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("jzweb:recruitmentNews:recruitmentNews")
    public Result<Page<RecruitmentNewsDO>> list(RecruitmentNewsDO recruitmentNewsDTO) {

        Wrapper<RecruitmentNewsDO> wrapper = new EntityWrapper<RecruitmentNewsDO>().orderBy("id", false);
        if (recruitmentNewsDTO.getState() != null) {
            wrapper.eq("state", recruitmentNewsDTO.getState());
        }
        if (recruitmentNewsDTO.getIsTop() != null) {
            wrapper.eq("is_top", recruitmentNewsDTO.getIsTop());
        }
        if( StringUtil.notEmpty(recruitmentNewsDTO.getName())){
            wrapper.like("name", recruitmentNewsDTO.getName());
        }


        String roleName = getRoleNames();//查询用户角色
        if (!roleName.contains("超级用户角色")) {
            wrapper.eq("org_id",this.getUser().getOrgId());
        }else {
            if (StringUtils.isNotBlank(recruitmentNewsDTO.getOrgName())){
                Wrapper<OrganizationNewsDO> orWrapper = new EntityWrapper<OrganizationNewsDO>().like("name",recruitmentNewsDTO.getOrgName());
                List<OrganizationNewsDO>  organizationNewsDOS = organizationNewsService.selectList(orWrapper);
                String str = "";
                for (OrganizationNewsDO organizationNewsDO : organizationNewsDOS){
                    str = str + organizationNewsDO.getId() + ",";
                }
                wrapper.and();
                if(StringUtils.isNotBlank(str)){
                    wrapper.in("org_id",str.substring(0,str.length()-1));
                }else{
                    wrapper.eq("org_id",null);
                }
            }
        }

        Page<RecruitmentNewsDO> page = recruitmentNewsService.selectPage(getPage(RecruitmentNewsDO.class), wrapper);
        List list = new ArrayList();
        for (int i = 0; i < page.getRecords().size(); i++) {
            RecruitmentNewsDO obj = (RecruitmentNewsDO) page.getRecords().get(i);
            if (obj.getOrgId() != null) {
                OrganizationNewsDO organizationNewsDO = organizationNewsService.selectById(obj.getOrgId());
                if (organizationNewsDO != null) {
                    obj.setOrgName(organizationNewsDO.getName());
                }
            }
            list.add(obj);
        }
        page.setRecords(list);
        return Result.ok(page);
    }

    /**
     * 批量审核
     * @return
     */
    @PostMapping ("/review")
    @ResponseBody
    @RequiresPermissions("jzweb:recruitmentNews:review")
   public Result<String>  review(@RequestParam("ids[]") Long[] ids,String remark,Integer state) {
        String rerutnS="";

        int i = 0;
        int j = 0;
        for (Long id : ids) {
            RecruitmentNewsDO recruitmentNewsDO = recruitmentNewsService.selectById(id);
            if(recruitmentNewsDO==null){
                return Result.build(0,"该信息未绑定企业，不能进行审核！" );

            }
                Wrapper<OrganizationRolesDO> wrapper = new EntityWrapper<OrganizationRolesDO>();

                wrapper.eq("org_id", recruitmentNewsDO.getOrgId());
                wrapper.eq("checker_state", 1);

          List list=  organizationRolesService.selectList(wrapper);
            if(list.size()>0){
                if(recruitmentNewsDO != null){
                    recruitmentNewsDO.setStatus(state);
                    recruitmentNewsDO.setCheckerId(this.getUserId());
                    recruitmentNewsDO.setCheckerDate(new Date());
                    if(state.equals(2)){
                        recruitmentNewsDO.setRemarks(remark);//未通过，添加备注
                    }else{
                        recruitmentNewsDO.setRemarks("");//审核通过，清空备注
                    }
                    boolean b = recruitmentNewsService.updateById(recruitmentNewsDO);
                    if (b) {
                        i++;
                    } else {
                        break;
                    }
                }
            }else {
                j++;
            }

        }

        if (i == ids.length) {
            return Result.build(0,"已成功审核：" + i + "条数据");
        } else {
            if(j>0){
                return Result.build(0,"已成功审核：" + i + "条数据"+"失败："+j+"失败原因，企业信息未进行审核！");
            }else {
                return Result.build(0,"已成功审核：" + i + "条数据");
            }

        }
    }

    @GetMapping("/add")
    @RequiresPermissions("jzweb:recruitmentNews:add")
    String add(Model model) {
        //查询用户角色
        String roleName = getRoleNames();
        if (!roleName.contains("超级用户角色")) {
            OrganizationNewsDO organizationNewsDO = organizationNewsService.selectById(this.getUser().getOrgId());
            model.addAttribute("orgRole", organizationNewsDO);
        }else{
            model.addAttribute("orgRole", organizationRolesService.selByRoleType(3));
        }
        Map map1money = new HashMap();
        map1money.put("Type","moneyList");
        List<DictDO>  listMoney=sysDictService.selectListBy(map1money);
        model.addAttribute("listMoney",listMoney);

        Map map1 = new HashMap();
        map1.put("Type","experience");

        List<DictDO>  experienceList=sysDictService.selectListBy(map1);
        model.addAttribute("experienceList",experienceList);
        return "jzweb/recruitmentNews/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("jzweb:recruitmentNews:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        //查询用户角色
        String roleName = getRoleNames();
        if (!roleName.contains("超级用户角色")) {
            OrganizationNewsDO organizationNewsDO = organizationNewsService.selectById(this.getUser().getOrgId());
            model.addAttribute("orgRole", organizationNewsDO);
        }else{
            model.addAttribute("orgRole", organizationRolesService.selByRoleType(3));
        }
        RecruitmentNewsDO recruitmentNews = recruitmentNewsService.selectById(id);
        model.addAttribute("recruitmentNews", recruitmentNews);
        Map map1money = new HashMap();
        map1money.put("Type","moneyList");
        List<DictDO>  listMoney=sysDictService.selectListBy(map1money);
        model.addAttribute("listMoney",listMoney);

        Map map1 = new HashMap();
        map1.put("Type","experience");

        List<DictDO>  experienceList=sysDictService.selectListBy(map1);
        model.addAttribute("experienceList",experienceList);
        return "jzweb/recruitmentNews/edit";
    }

    @GetMapping("/info/{id}")
    @RequiresPermissions("jzweb:recruitmentNews:recruitmentNews")
    String info(@PathVariable("id") Long id, Model model) {
        RecruitmentNewsDO recruitmentNews = recruitmentNewsService.selectById(id);
        if (recruitmentNews.getOrgId() != null) {
            OrganizationNewsDO organizationNewsDO = organizationNewsService.selectById(recruitmentNews.getOrgId());
            if (organizationNewsDO != null) {
                recruitmentNews.setOrgName(organizationNewsDO.getName());
            }
        }
        model.addAttribute("recruitmentNews", recruitmentNews);
        return "jzweb/recruitmentNews/info";
    }

    @Log("添加招聘信息")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("jzweb:recruitmentNews:add")
    public Result<String> save(RecruitmentNewsDO recruitmentNews) {
      //  recruitmentNews.setOrgId(this.getUser().getOrgId());
        //recruitmentNews.setSalaryRange(recruitmentNews.getMinMoney()+"_"+recruitmentNews.getMaxMoney());

        recruitmentNewsService.insert(recruitmentNews);
        return Result.ok();
    }

    @Log("修改招聘信息")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("jzweb:recruitmentNews:edit")
    public Result<String> update(RecruitmentNewsDO recruitmentNews) {

       // recruitmentNews.setSalaryRange(recruitmentNews.getMinMoney()+"_"+recruitmentNews.getMaxMoney());
        boolean update = recruitmentNewsService.updateById(recruitmentNews);
        return update ? Result.ok() : Result.fail();
    }

    @Log("删除招聘信息")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("jzweb:recruitmentNews:remove")
    public Result<String> remove(Long id) {
        recruitmentNewsService.deleteById(id);
        return Result.ok();
    }

    @Log("批量删除招聘信息")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("jzweb:recruitmentNews:batchRemove")
    public Result<String> remove(@RequestParam("ids[]") Long[] ids) {
        recruitmentNewsService.deleteBatchIds(Arrays.asList(ids));
        return Result.ok();
    }

    @Log("岗位上线")
    @PostMapping("/batchUpper")
    @ResponseBody
    @RequiresPermissions("jzweb:courses:batchRemove")
    public Result<String> batchUpper(@RequestParam("ids[]") Long[] ids) {
        List<RecruitmentNewsDO> recruitmentNewsDOS = new ArrayList<>();
        for (Long id : ids) {
            RecruitmentNewsDO recruitmentNewsDO = recruitmentNewsService.selectById(id);
            recruitmentNewsDO.setState(0);
            recruitmentNewsDO.setUpdateDate(new Date());
            recruitmentNewsDOS.add(recruitmentNewsDO);
        }
        boolean bbc = recruitmentNewsService.updateBatchById(recruitmentNewsDOS, recruitmentNewsDOS.size());
        return bbc ? Result.ok() : Result.fail();
    }

    @Log("岗位下线")
    @PostMapping("/batchLower")
    @ResponseBody
    @RequiresPermissions("jzweb:courses:batchRemove")
    public Result<String> batchLower(@RequestParam("ids[]") Long[] ids) {
        List<RecruitmentNewsDO> recruitmentNewsDOS = new ArrayList<>();
        for (Long id : ids) {
            RecruitmentNewsDO recruitmentNewsDO = recruitmentNewsService.selectById(id);
            recruitmentNewsDO.setState(1);
            recruitmentNewsDO.setUpdateDate(new Date());
            recruitmentNewsDOS.add(recruitmentNewsDO);
        }
        boolean bbc = recruitmentNewsService.updateBatchById(recruitmentNewsDOS, recruitmentNewsDOS.size());
        return bbc ? Result.ok() : Result.fail();
    }

}
